from tkinter import *
from tkinter import messagebox

# 三种模式，command回调，绑定事件，protocol监听
root = Tk()

def callback(event):
    print('frame clicked')

def key(event):
    print('pressed-', event.char)

def closeWindow():
    if messagebox.askokcancel('Quit', 'Do you want to quit?'):
        root.destroy()

frame = Frame(root, width=200, height=200)
frame.bind('<Button-1>', callback)
frame.bind("<KeyPress>", key)
frame.focus_force() # very important
frame.pack()

root.protocol('WM_DELETE_WINDOW', closeWindow)
root.mainloop()